import java.util.LinkedList;

public class L104 {

    class Solution {
        public int maxDepth(TreeNode root) {
            LinkedList<TreeNode> queue = new LinkedList<>();
            int deep = 0;
            queue.offer(root);
            while (!queue.isEmpty()) {
                int size = queue.size();
                while (size > 0) {
                    size--;
                    TreeNode poll = queue.poll();
                    if (poll == null) {
                        continue;
                    }
                    queue.offer(poll.left);
                    queue.offer(poll.right);
                }
                deep++;
            }
            return deep - 1;
        }
    }
}
